package lion.framework.core.web.request;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import lion.dev.lang.MapJ;

public class RequestSession extends MapJ {

	private static final long serialVersionUID = -1688889078370797892L;

	private HttpSession session;

	public RequestSession(HttpSession session) {

		this.session = session;

		List<String> keys = Collections.list(session.getAttributeNames());
		for (String key : keys) {
			super.put(key, session.getAttribute(key));
		}
	}

	@Override
	public Object put(String key, Object value) {

		this.session.setAttribute(key, value);

		return super.put(key, value);
	}

	@Override
	public void putAll(Map<? extends String, ? extends Object> param) {

		for (String key : param.keySet()) {
			this.session.setAttribute(key, param.get(key));
		}

		super.putAll(param);
	}

	@Override
	public Object remove(Object key) {

		this.session.removeAttribute(String.valueOf(key));

		return super.remove(key);
	}

	@Override
	public void clear() {

		List<String> keys = Collections.list(session.getAttributeNames());
		for (String key : keys) {
			session.removeAttribute(key);
		}
		super.clear();
	}
}
